#include "aichatdialog.h"
#include "ui_aichatdialog.h"

AIChatDialog::AIChatDialog(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AIChatDialog), aiServer(new AIServer(this))
{
    ui->setupUi(this);
    setWindowTitle("DeepSeek AI 助手");
       resize(600, 500);

       // 初始化 UI
       chatDisplay = new QTextEdit(this);
       chatDisplay->setReadOnly(true);
       chatDisplay->setPlaceholderText("AI 回复将显示在这里...");

       inputEdit = new QTextEdit(this);
       inputEdit->setMaximumHeight(100);
       inputEdit->setPlaceholderText("请输入问题...");

       sendButton = new QPushButton("发送", this);
       sendButton->setFixedWidth(80);

       // 布局
       QVBoxLayout *mainLayout = new QVBoxLayout(this);
       QHBoxLayout *inputLayout = new QHBoxLayout();

       mainLayout->addWidget(new QLabel("AI 回复:", this));
       mainLayout->addWidget(chatDisplay);
       mainLayout->addWidget(new QLabel("你的输入:", this));
       inputLayout->addWidget(inputEdit);
       inputLayout->addWidget(sendButton);
       mainLayout->addLayout(inputLayout);

       setLayout(mainLayout);

       // 信号槽连接
       connect(sendButton, &QPushButton::clicked, this, &AIChatDialog::onSendClicked);
       connect(inputEdit, &QTextEdit::textChanged, [this]() {
           if (inputEdit->toPlainText().endsWith("\n")) {
               onSendClicked(); // 支持 Ctrl+Enter 发送
           }
       });
       connect(aiServer, &AIServer::responseReceived, this, [this](const QString &reply) {
           chatDisplay->append("<b>AI:</b> " + reply);
       });
}

void AIChatDialog::onSendClicked() {
    QString text = inputEdit->toPlainText().trimmed();
    if (text.isEmpty()) return;

    chatDisplay->append("<b>你:</b> " + text);
    inputEdit->clear();
    aiServer->sendRequest(text); // 发送请求
}

AIChatDialog::~AIChatDialog()
{
    delete ui;
}
